﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BuscadorDePreco.DataContract;
using System.Transactions;

namespace BuscadorDePreco.Data
{
    public class CategoriaTableAdapter
    {

        public int AdicionarCategoria(CategoriaData categoria)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            int id = -1;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    Categoria cat = CategoriaData.ToCategoriaObj(categoria);

                    buscadorEntities.Categoria.AddObject(cat);
                    buscadorEntities.SaveChanges();

                    id = cat.Id;
                }
                scope.Complete();
            }

            return id;
        }

        public List<CategoriaData> RecuperarCategorias()
        {
            List<CategoriaData> categoriaList = new List<CategoriaData>();

            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var categorias = (from emp in buscadorEntities.Categoria
                              select emp).ToList();

                foreach (Categoria categ in categorias)
                    categoriaList.Add(CategoriaData.ToCategoriaData(categ));
            }

            return categoriaList;
        }

        public void ExcluirCategoria(int id)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var categoria = (from categ in buscadorEntities.Categoria
                                    where categ.Id == id
                                    select categ).ToList();

                    if (categoria != null && categoria.Count > 0)
                    {
                        buscadorEntities.Categoria.DeleteObject(categoria.First());
                        buscadorEntities.SaveChanges();
                    }
                }
                scope.Complete();
            }
        }

        public CategoriaData RecuperarCategoria(int id)
        {
            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var categ = (from cat in buscadorEntities.Categoria
                                 where cat.Id == id
                                 select cat);

                if (categ != null && categ.Count() > 0)
                    return CategoriaData.ToCategoriaData(categ.First());
            }

            return null;
        }

        public void AtualizarCategoria(CategoriaData newData)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var categoria = from cat in buscadorEntities.Categoria
                                      where cat.Id == newData.Id
                                      select cat;

                    if (categoria != null && categoria.Count() > 0)
                    {
                        Categoria c = categoria.First();
                        c.Nome = newData.Nome;
                    }

                    buscadorEntities.SaveChanges();

                }
                scope.Complete();
            }
        }
    }
}